#include "pch.h"



#define HOME_DIR "/tmp/tmp.FdUHWCPhyb/V2"

#ifdef MY_COMPILE_TIME
extern const char* my_compile_time = MY_COMPILE_TIME;
#else
const char* my_compile_time = "Unknown";
#endif

// 信号处理函数
extern "C" void my_signalHandler(int signal) {
    // 停止时开始统计
    std::cout << "停止信号处理函数" << std::endl;
    // 退出程序
    exit(0);
}

void exitFunction() {
    // 执行统计计算逻辑
    std::cout << "退出函数回调." << std::endl;
    //ntp::FileUtils fu;
    //fu.ReadFileAndStatistics();
}

int main(int argc, char *argv[]) {
    std::cout << "程序基本信息：编译时间：[" << my_compile_time << "]" << std::endl;
    // 参数校验
    std::cout << "参数个数：" << argc << " 第一个参数：" << argv[0] << ",第二个参数：" << argv[1] << std::endl;
    if (argc < 2) {
        std::cerr << "请输入交易所简称" << std::endl;
        return 1;
    }
    // 将信号处理函数注册为处理 SIGINT 信号
    std::signal(SIGINT, my_signalHandler);
    std::atexit(exitFunction); // 使用std::atexit注册函数，需要一个不带参数且返回类型为void类型地函数指针


    // 读取csv文件并处理每一行数据
    // 例如我们虚构一个品种，假设它的基准价是5000元/吨，我们每0.5秒钟随机给它＋1元、-1元或者保持原状。
    // 然后将这个品种的走势图连起来，就会形成一个和真实的期货品种完全一样的行情图


    //std::string fileName = iniCfg->ReadString("MODE", "txt_file_save_path","");
    std::string fileName = "delayFile.txt";
    std::ifstream file(fileName);
    if (!file.is_open()) {
        std::cerr << "无法打开文件" << std::endl;
    }

    // 延时容器
    std::vector<long long> delayList1;
    std::vector<long long> delayList2;

    long long timTmp = 0;
    int l1FastCount = 0; // l1快的包数
    int l2FastCount = 0; // l1快的包数
    while (!file.eof()) {
        std::string line;
        getline(file, line);
        if (line.empty()) {
            continue;
        }

        // 按逗号分割行
        std::vector<std::string> fields = splitByDelimiter(line, ',');
        // 总数
        //std::cout << "总字段 " << fields.size() << std::endl;

        //
        if ( fields[3] != "0" && fields[3] != "") {
            // 字符串转数字
            // 使用std::stoll将字符串转换为long long类型
            // 使用std::stol将字符串转换为long类型
            // 使用std::stoi将字符串转换为int类型
            //std::cout << "要转换的数字=" << fields[3] << std::endl;
            delayList1.insert(delayList1.end(), std::stoll(fields[3]));
            //delayList1.insert(delayList1.end(), std::stoi(fields[8]));
            // 字符串转数字，包含正数和负数
            //timTmp = std::stoi(fields[8]);
            //delayList1.push_back(std::stoi(fields[8]));
            //std::cout << timTmp << std::endl;
            // 字符串转数字，兼容正数和负数
        }

        // 判断字符串是否相等
        if ( fields[5] == "l1") {
            l1FastCount++;
        } else {
            l2FastCount++;
        }

        // 提取需要的字段
        //std::cout << "统计=" << splitByDelimiter(fields[1], '|')[0] << " " << std::endl;
        //std::cout
        //<< "总包数 " << splitByDelimiter(fields[2], '|')[0] << " "
        //<< splitByDelimiter(fields[3], '|')[0] << " "
        //<< splitByDelimiter(fields[1], '|')[0] << " "
        //<< splitByDelimiter(fields[1], '|')[0] << " "
        //<< splitByDelimiter(fields[1], '|')[0] << " "
        //<< splitByDelimiter(fields[1], '|')[0] << " "
        //<< "秒延时差=" << fields[7] << " "
        //<< "纳秒延时差=" << fields[8] << " "
        //<< "先到包数=" << fields[9] << " "
        //<< "快到的包数=" << fields[10] << " " // 快到的包数
        //<< "=" << fields[11] << " " // 那路快
        //<< "\n";
        ////// 暂停3秒
        //std::this_thread::sleep_for(std::chrono::seconds(3));
        // 将QuoteLV1 与QuoteLV2 按时间戳加合约代码为主键进行合并，得到28629条数据
        //std::cout << "当前容器大小=" << delayList1.size() << std::endl;
    }

    // 取std::vector容器的最大值
    // https://blog.csdn.net/weixin_44891861/article/details/123014807
    //long long maxValue = *std::max_element(delayList1.begin(), delayList1.end());
    // 最小值
    //long long minValue = *std::min_element(delayList1.begin(), delayList1.end());
    // 平均值 = std::accumulate 计算所有的元素之和，再除以容器的大小
    long long avgValue = std::accumulate(delayList1.begin(), delayList1.end(), static_cast<double>(0.0)) / delayList1.size();

    //std::cout << "最大值：" << m << std::endl;

    //std::cout << "最大值：" << maxValue
    //<< ", 最小值：" << minValue
    //<< ", 平均值：" << avgValue
    //<<  ", 总数：" << delayList1.size()
    //<< ", l1:" << l1FastCount << " " << (l1FastCount*1.0/delayList1.size())*100
    //<< ", l2:" << l2FastCount << " " << (l2FastCount*1.0/delayList1.size())*100
    //<< std::endl;

    // 写入文件
    std::ofstream outFile("outFile.txt", std::ios::app);
    outFile
    << "交易所:" << argv[1]
    << ", 计算日期: " << currTime()
    << ", 总数:" << delayList1.size()*1001
    << ", a路先到:" << l1FastCount*1001
    << ", a占比:" << (l1FastCount*1.0/delayList1.size())*100
    << ", b路先到:" << l2FastCount*1001
    << ", b占比:" << (l2FastCount*1.0/delayList1.size())*100
    << ", 平均延迟:" << avgValue
    << std::endl;
    outFile.close();
}
